﻿using IServices;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using YL.Core.Dto;
using YL.Core.Entity;
using YL.Core.Entity.Fluent.Validation;
using YL.Core.Entity.Meter;
using YL.NetCore.Attributes;
using YL.NetCore.NetCoreApp;
using YL.Utils.Extensions;
using YL.Utils.Pub;
using YL.Utils.Table;

namespace Method.SmartMeter.Controllers
{
    public class InspectionController : BaseController
    {
        private readonly IMeter_InspectionServices _inspectionServices;
        private readonly IMeter_DeviceDataServices _deviceDataServices;
        private readonly IMeter_InspectionHistoryServices _inspectionHistoryServices;

        public InspectionController(IMeter_InspectionServices inspectionServices, 
            IMeter_InspectionHistoryServices inspectionHistoryServices,
            IMeter_DeviceDataServices deviceDataServices)
        {
            _inspectionServices = inspectionServices;
            _inspectionHistoryServices = inspectionHistoryServices;
            _deviceDataServices = deviceDataServices;
        }

        [HttpGet]
        [CheckMenu]
        public IActionResult Index(string ClientName = "",string DeviceName="")
        {
            ViewBag.ClientName = ClientName;
            ViewBag.DeviceName = DeviceName;
            return View();
        }

        [HttpPost]
        [OperationLog(LogType = LogType.select)]
        public ContentResult List([FromForm] Bootstrap.BootstrapParams bootstrap)
        {
            bootstrap.UserId = UserDto.UserId;
            var sd = _inspectionServices.PageList(bootstrap);
            return Content(sd);
        }


        [HttpGet]
        public IActionResult Add(string ClientId, string DeviceId, string ClientName, string DeviceName)
        {
            ViewBag.ClientId = ClientId;
            ViewBag.DeviceId = DeviceId;
            ViewBag.ClientName = ClientName;
            ViewBag.DeviceName = DeviceName;

            List<DeviceData> deviceDatas = _deviceDataServices.QueryableToList(s=>s.DeviceId== DeviceId.ToInt64());
            return View(deviceDatas);
        }

        [HttpPost]
        [FilterXss]
        [OperationLog(LogType = LogType.addOrUpdate)]
        public IActionResult AddOrUpdate([FromForm] Inspection inspection)
        {

            InspectionHistory inspectionHistory = new InspectionHistory();
            inspectionHistory.ClientId = inspection.ClientId;
            inspectionHistory.DeviceId = inspection.DeviceId;
            inspectionHistory.UserId = UserDtoCache.UserId;

            inspectionHistory.Inspector = inspection.Inspector;
            inspectionHistory.InspectionTime = DateTime.Now;
            inspectionHistory.CreateTime = DateTime.Now;

            inspectionHistory.IsNormal = inspection.IsNormal;
            inspectionHistory.Remark = inspection.Remark;
            
            bool flagHistory = _inspectionHistoryServices.Insert(inspectionHistory);

            return BootJsonH(flagHistory ? (flagHistory, PubConst.Success) : (flagHistory, PubConst.Fail));

        }
    }
}